<?php

namespace App\Listeners;

use App\Events\LeaveCar;
use Carbon\Carbon;
use App\Models\FlowCollection;
use Illuminate\Contracts\Queue\ShouldQueue;


/**
 * 出车车流统计
 *
 * Class AddFlowNumber
 * @package App\Listeners
 */
class AddFlowNumber implements ShouldQueue
{

    public function handle(LeaveCar $event)
    {
        $park_code = $event->data->park_code;
        $dateTime = Carbon::createFromTimestamp($event->data->out_time);
        $hour = Carbon::create($dateTime->year, $dateTime->month, $dateTime->day, $dateTime->hour);
        $flowCollection = FlowCollection::firstOrNew(
            [
                'park_code' => $park_code,
                'hour' => $hour
            ],
            [
                'leave_flow' => 0,
                'short_time_free' => 0,
                'self_free' => 0,
                'worker_car' => 0,
                'inner_car' => 0,
                'free_car' => 0,
                'blank_car' => 0,
                'other_free' => 0,
                'free_car_total' => 0,
                'big_car' => 0,
                'taxi' => 0,
                'nev' => 0,
                'small_car' => 0,
                'other_charge_car' => 0,
                'etc_charge_car' => 0,
                'etc_free_car' => 0,
                'etc_small_car' => 0,
                'etc_big_car' => 0,
            ]
        );

        $flowCollection->leave_flow += 1;

        //免费车
        $vplType = $event->data->vpl_type;
        if ($event->data->parking_charge < 1) {
            //收费项
            $items = $event->data->charge_items;
            if($event->data->etc_flag){
                $flowCollection->etc_free_car += 1;
            }
            if (count($items) > 0) {
                foreach ($items as $v) {
                    //手动抬干
                    if ($v->charge_type == 8) {
                        $vplType = -1;
                    }
                    //短时苗木费
                    if ($v->charge_type == 0) {
                        $vplType = -2;
                    }
                }
            }
            switch ($vplType) {
                case -1://手动抬干
                    $flowCollection->self_free += 1;
                    break;
                case -2://短时免费
                    $flowCollection->short_time_free += 1;
                    break;
                case 8:  //优惠车
                    $flowCollection->worker_car += 1;
                    break;
                case 10:// 免费车
                    $flowCollection->inner_car += 1;
                    break;
                case 9:// 员工车
                    $flowCollection->free_car += 1;
                    break;
                case 1:// 白名单
                    $flowCollection->blank_car += 1;
                    break;
                default:
                    $flowCollection->other_free += 1;

            }

            $flowCollection->free_car_total += 1;

        } else {
            if($event->data->etc_flag){
                $flowCollection->etc_charge_car += 1;
            }
            //收费车
            $vpl_color=$event->data->vpl_color;
            switch ($vpl_color) {
                case 0:
                    $flowCollection->small_car += 1;
                    $flowCollection->etc_small_car += 1;
                    break;
                case 1:
                    $flowCollection->big_car += 1;
                    $flowCollection->etc_big_car += 1;
                    break;
                default:
                    $flowCollection->other_charge_car += 1;
            }
            if($event->data->park_code == '7100000319'){
                $flowCollection->taxi += 1;
            }

            $flowCollection->total_charge_car += 1;
        }

        // 新能源车
        if ($event->data->vpl_color==4) {
            $flowCollection->nev += 1;
        }

        $flowCollection->save();
    }
}
